package top.dark.ware.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.Query;

import top.dark.common.utils.R;
import top.dark.ware.dao.WareSkuDao;
import top.dark.ware.entity.WareSkuEntity;
import top.dark.ware.feign.ProductFeignService;
import top.dark.ware.service.WareSkuService;
import top.dark.ware.vo.SkuStockVo;

@Slf4j
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;
    @Autowired
    private WareSkuDao wareSkuDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSkuEntity> wareSkuEntities = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities != null && wareSkuEntities.size() > 0) {
            // 更新
            wareSkuDao.updateWareSku(skuId,wareId,skuNum);
        } else {
            // 新增
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            wareSkuEntity.setWareId(wareId);
            // 通过远程调用mall-product服务获取
            try {
                R info = productFeignService.info(skuId);
                if (info != null && info.getCode() == 0) {
                    Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                    wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
                }
            } catch (Exception e) {
                log.error("远程调用mall-product服务获取sku信息异常,异常信息：{}", e);
            }
            // TODO 后续完善，使用其他方法获取
            this.baseMapper.insert(wareSkuEntity);
        }
    }

    @Override
    public List<SkuStockVo> skuHasStock(List<Long> skuIds) {
        List<SkuStockVo> skuStockVos = skuIds.stream().map(skuId -> {
            SkuStockVo vo = new SkuStockVo();
            vo.setSkuId(skuId);
            vo.setHasStock(false);
            Long stock = baseMapper.skuHasStock(skuId);
            if (stock != null && stock > 0) {
                vo.setHasStock(true);
            }
            return vo;
        }).collect(Collectors.toList());
        return skuStockVos;
    }

}